﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Dukou.Javascript
{
    public class JsComparisonExpr : JsExpr
    {
        public object Left { get; private set; }

        public object Right { get; private set; }

        public JsComparisonOperator ComparisonOperator { get; set; }

        public JsComparisonExpr(object left, object right, JsComparisonOperator opt)
        {
            this.Left = left;
            this.Right = right;
            this.ComparisonOperator = opt;
        }

        public override string ToString()
        {
            JsVar var1 = this.Left as JsVar;
            JsVar var2 = this.Right as JsVar;

            return string.Format("{0} {1} {2}", var1 == null ? JsValue.Create(this.Left).ToString() : var1.Name
                , GetOperator(this.ComparisonOperator)
                , var2 == null ? JsValue.Create(this.Right).ToString() : var2.Name);
        }

        public static string GetOperator(JsComparisonOperator opt)
        {
            switch (opt)
            {
                case JsComparisonOperator.Eq:
                    return "==";
                case JsComparisonOperator.Ge:
                    return ">=";
                case JsComparisonOperator.Le:
                    return "<=";
                case JsComparisonOperator.Gt:
                    return ">";
                case JsComparisonOperator.Lt:
                    return "<";
                case JsComparisonOperator.Ne:
                    return "!=";
                default:
                    return "==";
            }
        }
    }
}
